package com.dq.Week3.day13.ddz;

import com.dq.Week3.day13.mail.MailInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class PukeCardUtils {

    // 声明扑克点数的数组
    public static final String[] POINTS_ARR = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","SJOKER","BJOKER"};

    // 声明扑克花色的数组
    public static final String[] PUKE_TYPE = {"♥","♠","♦","♣"};

    private ArrayList<PukeCard> p1 = new ArrayList<>();
    private ArrayList<PukeCard> p2 = new ArrayList<>();
    private ArrayList<PukeCard> p3 = new ArrayList<>();
    private ArrayList<PukeCard> d1 = new ArrayList<>();


    public ArrayList<PukeCard> getP1() {
        return p1;
    }

    public void setP1(ArrayList<PukeCard> p1) {
        this.p1 = p1;
    }

    public ArrayList<PukeCard> getP2() {
        return p2;
    }

    public void setP2(ArrayList<PukeCard> p2) {
        this.p2 = p2;
    }

    public ArrayList<PukeCard> getP3() {
        return p3;
    }

    public void setP3(ArrayList<PukeCard> p3) {
        this.p3 = p3;
    }

    public ArrayList<PukeCard> getD1() {
        return d1;
    }

    public void setD1(ArrayList<PukeCard> d1) {
        this.d1 = d1;
    }

    /**
     * 实例方法洗牌
     * @param cards
     */
    public void drawCards(List<PukeCard> cards){

        //1. 洗牌
        Collections.shuffle(cards);

        //2. 发牌给三位玩家，但是要剩三张底牌
        int remain = cards.size();

        //3. 把cards转换为 栈
        LinkedList<PukeCard> stack = new LinkedList<>(cards);

        while (remain>3){ // 当剩余牌的数量大于3时，就发牌
            p1.add(stack.pop()); // 给1号发牌
            p2.add(stack.pop()); // 给1号发牌
            p3.add(stack.pop()); // 给1号发牌
            remain = remain-3;
        }

        //4. 处理底牌
        d1.addAll(stack);

    }


    /**
     * 模拟斗地主抓牌
     * @param cards  54张牌
     * @param p1  玩家1的手牌
     * @param p2
     * @param p3
     * @param d1  底牌，剩三张
     */
    public static void drawPukeCard(List<PukeCard> cards,List<PukeCard> p1,List<PukeCard> p2,
                                    List<PukeCard> p3,List<PukeCard> d1){

        //1. 洗牌
        Collections.shuffle(cards);

        //2. 发牌给三位玩家，但是要剩三张底牌
        int remain = cards.size();

        //3. 把cards转换为 栈
        LinkedList<PukeCard> stack = new LinkedList<>(cards);

        while (remain>3){ // 当剩余牌的数量大于3时，就发牌
            p1.add(stack.pop()); // 给1号发牌
            p2.add(stack.pop()); // 给1号发牌
            p3.add(stack.pop()); // 给1号发牌
            remain = remain-3;
        }

        //4. 处理底牌
        d1.addAll(stack);

    }

    /**
     * 创建一副扑克牌
     * @return
     */
    public static List<PukeCard> createPukeList(){

        ArrayList<PukeCard> pukeCards = new ArrayList<>();

        for (int i = 0; i < POINTS_ARR.length-2; i++) {
            for (String type : PUKE_TYPE) {
                PukeCard pukeCard = new PukeCard(type, POINTS_ARR[i], i);
                pukeCards.add(pukeCard);
            }
        }
        // 单独处理大小王
        PukeCard sjoker = new PukeCard("", "SJOKER", 13);
        PukeCard bjoker = new PukeCard("", "BJOKER", 14);
        pukeCards.add(sjoker);
        pukeCards.add(bjoker);


        return pukeCards;
    }

    public static void m1(int a){
        a = a*2;
        System.out.println(a);
    }

    public static void m2(int[] arr){
        arr[0] = arr[0]+1;
        System.out.println(arr[0]);
    }

    public static void m3(MailInfo mailInfo){
        mailInfo.setTitle("xxxxx");
        System.out.println(mailInfo.getTitle());
    }



    public static void main(String[] args) {

//        MailInfo mailInfo = new MailInfo();
//        mailInfo.setTitle("abc");
//        m3(mailInfo);
//        System.out.println(mailInfo.getTitle());

//        int[] arr = {10,5};
//        m2(arr);
//        System.out.println(arr[0]);

//        int c = 1000;
//        m1(c);
//        System.out.println(c);

        List<PukeCard> pukeList = createPukeList();

        // 记住：要调用实例方法，一定要先创建对象
        // 创建对象的过程中，实例属性都应该被正确的初始化
        test2(pukeList);

//        test1();

    }

    private static void test2(List<PukeCard> pukeList) {
        PukeCardUtils pukeCardUtils = new PukeCardUtils();
        pukeCardUtils.drawCards(pukeList);

        ArrayList<PukeCard> p1List = pukeCardUtils.getP1();
        // 如何对手牌进行排序
        Collections.sort(p1List,(a,b)-> a.getValue()-b.getValue());
        System.out.println("p1:"+p1List);
        ArrayList<PukeCard> p2List = pukeCardUtils.getP2();
        Collections.sort(p2List,(a,b)-> a.getValue()-b.getValue());
        System.out.println("p2:"+p2List);
        ArrayList<PukeCard> p3List = pukeCardUtils.getP3();
        Collections.sort(p3List,(a,b)-> a.getValue()-b.getValue());
        System.out.println("p3:"+p3List);
        ArrayList<PukeCard> d1List = pukeCardUtils.getD1();
        System.out.println(d1List);

        // p1叫地主
        p1List.addAll(d1List);
        System.out.println(p1List);

    }

    private static void test1() {
        List<PukeCard> pukeList = createPukeList();
//        for (PukeCard pukeCard : pukeList) {
//            System.out.println(pukeCard);
//        }
        System.out.println(pukeList.size());

        ArrayList<PukeCard> p1 = new ArrayList<>();
        ArrayList<PukeCard> p2 = new ArrayList<>();
        ArrayList<PukeCard> p3 = new ArrayList<>();
        ArrayList<PukeCard> d1 = new ArrayList<>();

        drawPukeCard(pukeList,p1,p2,p3,d1);

        System.out.println("玩家1："+p1);
        System.out.println("玩家2："+p2);
        System.out.println("玩家3："+p3);
        System.out.println("底牌："+d1);
    }


}